package com.atguigu.demo03;

import ch.qos.logback.core.pattern.FormatInfo;

import java.util.concurrent.*;

/**
 * @author gjl
 * @create 2021-04-26 22:52
 * 自己创建配置线程池
 */
public class MyThreadPoolDemo {
    public static void main(String[] args) {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                2,
                5,
                1L,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(3),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.DiscardOldestPolicy());

        // 工作线程数   总共线程数  工作时间   时间单位    阻塞队列（等候大厅） 线程工厂  拒绝策略

        //ExecutorService service = Executors.newFixedThreadPool(6);//固定长度线程池
        //ExecutorService service = Executors.newSingleThreadExecutor();//一池一线程
        try {
            for (int i = 0; i < 8; i++) {
                int finalI = i;
                threadPoolExecutor.submit(()->{
                    System.out.println(Thread.currentThread().getName()+"给"+ finalI +"办理业务");
                    try {
                        TimeUnit.SECONDS.sleep(3);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                });
            }
        } finally {
            threadPoolExecutor.shutdown();
        }
    }

}
